//
//  HNSH_ChangeNameViewController.m
//  HNSH_SingleProject
//
//  Created by 李宇廷 on 2023/2/28.
//

#import "HNSH_ChangeNameViewController.h"

@interface HNSH_ChangeNameViewController ()<UITextFieldDelegate>
/** */
@property (nonatomic, strong)UIButton * saveBtn;
/** */
@property (nonatomic, strong)UILabel * desLabel;
/** */
@property (nonatomic, strong)UIView * bgView;
/** */
@property (nonatomic, strong)UITextField * textField;
/** */
@property (nonatomic, strong)UILabel * numLabel;

@end

@implementation HNSH_ChangeNameViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self showNavTitle:@"修改昵称" showLeftItem:YES];
    
    self.view.backgroundColor = [UIColor appWhiteLabelColor];
    
    [self addAllViews];
    [self addAllLayout];
}


- (void)addAllViews{
    [self.view addSubview:self.desLabel];
    [self.view addSubview:self.bgView];
    [self.bgView addSubview:self.textField];
    [self.bgView addSubview:self.numLabel];
    
    [self.view addSubview:self.saveBtn];
}

- (void)addAllLayout{
    [self.desLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(28);
        make.top.mas_equalTo(kNavBarAndStatusBarHeight + 32);
        make.height.mas_equalTo(20);
    }];
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.mas_equalTo(self.desLabel.mas_bottom).offset(14);
        make.height.mas_equalTo(50);
    }];
    [self.textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(28);
        make.top.bottom.mas_equalTo(0);
        make.right.mas_equalTo(-100);
    }];
    [self.numLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-28);
        make.centerY.mas_equalTo(0);
        make.height.mas_equalTo(20);
    }];
}

//MARK: Custom Methods
- (void)saveNickNameAction{
    [self.view endEditing:YES];
    
    if (![self.textField.text isNotBlank]) {
        [self.view makeToast:@"请输入昵称"];
        return;
    }
    NSDictionary * params = @{@"nickname":self.textField.text};
    [HNSH_UserNetServer requestUserUpdateWithParams:params success:^(ST_BaseNetModel * _Nullable responseObject) {
        if (responseObject.code == 0) {
            [kSTWindow makeToast:@"保存成功"];
            [ST_UserManager shareInstance].user.nickname = self.textField.text;
            [self.navigationController popViewControllerAnimated:YES];
        }
    } Failure:^(NSError * _Nullable error) {
        
    }];
}

- (void)clearBtnAction{
    self.textField.text = nil;
}

//MARK: Getter Methods
- (UIButton *)saveBtn{
    if (!_saveBtn) {
        _saveBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _saveBtn.frame = CGRectMake(30, kNavBarAndStatusBarHeight+268, kScreenWidth-60, 50);
        [_saveBtn setTitle:@"保存" forState:UIControlStateNormal];
        [_saveBtn setTitleColor:[UIColor appWhiteLabelColor] forState:UIControlStateNormal];
        _saveBtn.titleLabel.font = [UIFont boldSystemFontOfSize:14];
        _saveBtn.backgroundColor = [UIColor appGreenColor];
        _saveBtn.layer.cornerRadius = 25;
        _saveBtn.layer.masksToBounds = YES;
        [_saveBtn addTarget:self action:@selector(saveNickNameAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _saveBtn;
}

- (UILabel *)desLabel{
    if (!_desLabel) {
        _desLabel = [UILabel new];
        _desLabel.font = [UIFont systemFontOfSize:14];
        _desLabel.textColor = [UIColor appGrayLabelColor];
        _desLabel.text = @"给自己取个喜欢的昵称吧，开启你的购物之旅";
    }
    return _desLabel;
}

- (UIView *)bgView{
    if (!_bgView) {
        _bgView = [UIView new];
        _bgView.backgroundColor = [UIColor whiteColor];
        _bgView.layer.cornerRadius = 8;
        _bgView.layer.masksToBounds = YES;
    }
    return _bgView;
}

- (UITextField *)textField{
    if (!_textField) {
        _textField = [[UITextField alloc] init];
        _textField.font = [UIFont systemFontOfSize:14];
        _textField.placeholder = @"请输入昵称";
        _textField.textColor = [UIColor appBlackLabelColor];
        _textField.delegate = self;
        _textField.clearButtonMode = UITextFieldViewModeWhileEditing;
        _textField.text = [ST_UserManager shareInstance].user.nickname;
        UIButton *pwdClean = [_textField valueForKey:@"_clearButton"]; //key是固定的
        [pwdClean setImage:[UIImage imageNamed:@"base_field_clear"] forState:UIControlStateNormal];
        [pwdClean setImage:[UIImage imageNamed:@"base_field_clear"] forState:UIControlStateHighlighted];
    }
    return _textField;
}

- (UILabel *)numLabel{
    if (!_numLabel) {
        _numLabel = [UILabel new];
        _numLabel.font = [UIFont systemFontOfSize:16];
        _numLabel.textColor = [UIColor appGrayLabelColor];
        _numLabel.text = [NSString stringWithFormat:@"%lu/15", (unsigned long)self.textField.text.length];
    }
    return _numLabel;
}

//MARK: UITextFieldDelegate Methods
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    NSString * currentStr = textField.text;
    currentStr = [currentStr stringByReplacingCharactersInRange:range withString:string];
    if (currentStr.length > 15) {
        return NO;
    }
    self.numLabel.text = [NSString stringWithFormat:@"%lu/15",(unsigned long)currentStr.length];
    return YES;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
